import 'dart:io';
import 'dart:ui';

import 'package:elevator/pages/login/login_view.dart';
import 'package:elevator/storage/local_storage.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:mysql1/mysql1.dart';
import 'package:package_info/package_info.dart';
import 'package:path_provider/path_provider.dart';
import 'package:elevator/get/intl_keys.dart';

import '../../get/message.dart';
import '../../mqtt/mqtt_manager.dart';
import 'setting_state.dart';
import 'package:mysql1/mysql1.dart' as mysql;

class SettingLogic extends GetxController {
  final SettingState state = SettingState();

  @override
  void onInit() {
    state.user.value = Get.arguments["user"];
    getVersion();
    super.onInit();
  }

  @override
  void onReady() {

    super.onReady();
  }

  changeLanguage() {
    debugPrint("selectLanguage:$state.selectLanguage.value");
    if(0==state.selectLanguage.value){
      Messages.change2CHLocal();
    }else{
      Messages.changeUSLocal();
    }
  }

  void selectLanguage(int selectedIndex) {
    state.selectLanguage.value = selectedIndex;
  }

  void cancelChangeLanguage() {
    var local = LocalStorage.get("Language");
    if('zh'==local){
      state.selectLanguage.value = 0;
    }else{
      state.selectLanguage.value = 1;
    }
  }

  void getVersion() async {
   PackageInfo packageInfo = await PackageInfo.fromPlatform();
   state.version.value = packageInfo.version;
  }

  logout() {
    saveToken("");
    Get.offAll(LoginPage());
  }

  clearFileCache()  async {
    Directory directory = await getTemporaryDirectory();
    String path = '${directory.path}${Platform.pathSeparator}img${Platform.pathSeparator}';
    var fileExist = Directory(path).existsSync();
    debugPrint('SettingLogic:缓存目录$path 是否存在 $fileExist');
    if(fileExist){
      debugPrint('SettingLogic:缓存目录$path 存在，执行删除');
      Directory(path).deleteSync(recursive:true);
      Get.snackbar(IntlKeys.updateImg.tr, IntlKeys.success.tr,snackPosition: SnackPosition.TOP);
      return;
    }else{
      Get.snackbar(IntlKeys.updateImg.tr, IntlKeys.success.tr,snackPosition: SnackPosition.TOP);
    }
  }

  void saveToken (String token) async{
    print("start saveToken");
    var settings = mysql.ConnectionSettings(
        host: '36.141.45.30',
        port: 43306,
        user: 'root',
        password: 'adminemqx123',
        db: 'RegInfo',
        useCompression: false,
        timeout:const Duration(seconds: 60),
        maxPacketSize:16 * 1024 * 1024 *1024,
        characterSet: CharacterSet.UTF8MB4);
    var conn;
    try{
      conn = await mysql.MySqlConnection.connect(settings);

      var result = await conn
          .query('update tbPassWord set tokenID=? where ID=?',[token,state.user.value.id]);
      debugPrint(result!.toString());
    }catch(e){
      await conn.close();
      saveToken(token);
    }
  }
}
